// pages/addActive/addActive.js
var util = require('../../utils/util.js');
//获取应用实例
const app = getApp()
var date = new Date();
var currentHours = date.getHours();
var currentMinute = date.getMinutes();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,     
    items: '',
    itemsImg: [],
    checkIsShow: wx.getStorageSync('checkIsShow'),
    //display: 'inline-block',
    redisplay: 'none',
    display: [['inline-block', 'none'], ['inline-block', 'none'], ['inline-block', 'none'], ['inline-block', 'none'], ['inline-block', 'none']],

    dates: ['', ''],

    startDate: "",

    multiArray: [[], [], []],
    multiIndex: [0, 0, 0],

    //设置input标签是否禁用
    status1: false,
    status2: false,

    //input内容
    numLow: '',
    timeLimitTime: '',
    timeQuitTime: '',
    text: '',


  },
  userInput: function (e) {
    this.setData({
      num: e.detail.cursor
    })
  },


  switchChange1: function (e) {
    var that = this;
    that.setData({
      status1: e.detail.value,
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var up = "display[" + 0 + "][[" + 0 + "]";
    var down = "display[" + 0 + "][[" + 1 + "]";
    this.setData({
      up: 'none',
      down: 'inline-block'
    })

  },

  //空值判断
  isempty: function (res, name) {
    console.log(res + "@");
    if (res == '' || res == null) {
      console.log("1")
      wx.showToast({
        title: name + "不能为空！",
      })
      return;
    }
  },
  //提交表单
  formSubmit: function (e) {
    var that = this;
    var formid = e.detail.formId;
    if (e.detail.value.numLow=="") {
      wx.showToast({
        title: '目标怎么会没有标题呢~',
        icon:"none"
      })
      return false;
    } 
    wx.request({
      url: `${app.globalData.apiurl}api/SendMess/saveformid`,
      method: 'POST',
      data: {
        uid: app.globalData.userInfo.uid,
        formid: formid
      },
      success(e) {

      }
    })
    if (that.data.startDate==""){
      var time = new Array();
    }else{
      //时间转时间戳
      var oldtime = that.data.startDate;
      console.log(oldtime);
      var oldyear = util.formatTime(date).substring(0, 4);
      var tt = oldtime.match(/(\w+)/g);
      if (Number(tt[0]) == 1 && Number(tt[1]) < 29 && Number(oldyear) == 2018) {
        oldyear = (Number(oldyear) + 1);
      }
      if (tt[0].length == 1) {
        tt[0] = "0" + tt[0];
      }
      if (tt[1].length == 1) {
        tt[1] = "0" + tt[1];
      }
      var newdate = oldyear + "-" + tt[0] + "-" + tt[1] + " " + tt[2] + ":" + tt[3] + ":" + "00";
      console.log(newdate);
      var timestamp = new Date(newdate.replace(/-/g, "/")).getTime() / 1000;
      console.log(timestamp);
      console.log(Number(e.detail.value.timeLimit));
      var time = new Array();
      time.push(timestamp)
    }
    


    console.log(time[0])
    wx.request({
      url: `${app.globalData.apiurl}api/index/addTask`,
      method: 'POST',
      data:
      {
        'notice_time': time,
        "uid": app.globalData.userInfo.uid,
        'is_import': Number(e.detail.value.timeLimit),
        'title': e.detail.value.numLow,
        "is_end":0,
        'text': e.detail.value.text,
      },
      success: (res) => {
        console.log(res)
        wx.showToast({
          title: '创建目标成功',
        })

        that.setData({
          num: 0,
          items: '',
          itemsImg: [],

          display: 'inline-block',
          redisplay: 'none',
          display: [['inline-block', 'none'], ['inline-block', 'none'], ['inline-block', 'none'], ['inline-block', 'none'], ['inline-block', 'none']],

          dates: ['', ''],

          startDate: "",

          multiArray: [[], [], []],
          multiIndex: [0, 0, 0],
          //设置input标签是否禁用
          status1: false,
          status2: false,
          numLow: '',
          numUp: '',
          timeLimitTime: '',
          timeQuitTime: '',
          money: '',
          text: '',
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }, 1000)
      }
    })

  },
  pickerTap: function () {
    date = new Date();

    var monthDay = ['今天', '明天'];
    var hours = [];
    var minute = [];

    currentHours = date.getHours();
    currentMinute = date.getMinutes();

    // 月-日
    for (var i = 2; i <= 7; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth() + 1) + "-" + date1.getDate();
      monthDay.push(md);
    }

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };

    if (data.multiIndex[0] === 0) {
      if (data.multiIndex[1] === 0) {
        this.loadData(hours, minute);
      } else {
        this.loadMinute(hours, minute);
      }
    } else {
      this.loadHoursMinute(hours, minute);
    }

    data.multiArray[0] = monthDay;
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;

    this.setData(data);
  },




  bindMultiPickerColumnChange: function (e) {
    date = new Date();

    var that = this;

    var monthDay = ['今天', '明天'];
    var hours = [];
    var minute = [];

    currentHours = date.getHours();
    currentMinute = date.getMinutes();

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    // 把选择的对应值赋值给 multiIndex
    data.multiIndex[e.detail.column] = e.detail.value;

    // 然后再判断当前改变的是哪一列,如果是第1列改变
    if (e.detail.column === 0) {
      // 如果第一列滚动到第一行
      if (e.detail.value === 0) {

        that.loadData(hours, minute);

      } else {
        that.loadHoursMinute(hours, minute);
      }

      data.multiIndex[1] = 0;
      data.multiIndex[2] = 0;

      // 如果是第2列改变
    } else if (e.detail.column === 1) {

      // 如果第一列为今天
      if (data.multiIndex[0] === 0) {
        if (e.detail.value === 0) {
          that.loadData(hours, minute);
        } else {
          that.loadMinute(hours, minute);
        }
        // 第一列不为今天
      } else {
        that.loadHoursMinute(hours, minute);
      }
      data.multiIndex[2] = 0;

      // 如果是第3列改变
    } else {
      // 如果第一列为'今天'
      if (data.multiIndex[0] === 0) {

        // 如果第一列为 '今天'并且第二列为当前时间
        if (data.multiIndex[1] === 0) {
          that.loadData(hours, minute);
        } else {
          that.loadMinute(hours, minute);
        }
      } else {
        that.loadHoursMinute(hours, minute);
      }
    }
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;
    this.setData(data);
  },

  loadData: function (hours, minute) {

    var minuteIndex;
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10;
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20;
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30;
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40;
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50;
    } else {
      minuteIndex = 60;
    }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = 0; i < 60; i += 10) {
        minute.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = minuteIndex; i < 60; i += 10) {
        minute.push(i);
      }
    }
  },

  loadHoursMinute: function (hours, minute) {
    // 时
    for (var i = 0; i < 24; i++) {
      hours.push(i);
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },

  loadMinute: function (hours, minute) {
    var minuteIndex;
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10;
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20;
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30;
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40;
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50;
    } else {
      minuteIndex = 60;
    }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },

  bindStartMultiPickerChange: function (e) {
    var that = this;
    var monthDay = that.data.multiArray[0][e.detail.value[0]];
    var hours = that.data.multiArray[1][e.detail.value[1]];
    var minute = that.data.multiArray[2][e.detail.value[2]];

    if (monthDay === "今天") {
      var month = date.getMonth() + 1;
      var day = date.getDate();
      monthDay = month + "月" + day + "日";
    } else if (monthDay === "明天") {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + 1);
      monthDay = (date1.getMonth() + 1) + "月" + date1.getDate() + "日";

    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      monthDay = month + "月" + day + "日";
    }
    if (hours < 10) {
      hours = '0' + hours;
    }
    if (minute < 10) {
      minute = '0' + minute;
    }
    var startDate = monthDay + " " + hours + ":" + minute;
    var endisplay = that.data.display;
    for (var i = 0; i < 1; i++) {
      for (var j = 0; j < 2; j++) {
        if (j == 0) endisplay[0][0] = 'none';
        if (j == 1) endisplay[0][1] = 'inline-block';
      }
    }
    that.setData({
      startDate: startDate,
      display: endisplay,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.pickerTap();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

}) 